/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lx_easyfinace.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lcn.
 * 计算辅助类
 */
public class CalculateUtil {

    /**
     * 计算数量总数
     * @param quantityStr 数量字符串(类似 "55.5+22.5+50" )
     * @return 相加的结果
     * @throws java.lang.NumberFormatException 当参数不符合上诉格式时,抛出此异常
     */
    public static float calculateQuantity(String quantityStr) throws java.lang.NumberFormatException {
        float quantity = 0;

        String[] split = quantityStr.split("\\+");
        for (int i = 0; i < split.length; i++) {
            if (split[i].contains("y")) {
//                System.out.println("是码");
                quantity = quantity + Float.parseFloat(split[i].trim().substring(0, split[i].trim().length() - 1)) * 0.9144f;
            } else {
                quantity = quantity + Float.parseFloat(split[i]);
            }
        }

        return quantity;
    }

    public static double formatNumber(double d) {
        DecimalFormat df = new DecimalFormat(".00");
        return Double.parseDouble(df.format(d));
    }

    public static float calculateAotoYardToMi(String yard) {
        if (yard.contains("y")) {
//            System.out.println("是码");
            return Float.parseFloat(yard.trim().substring(0, yard.trim().length()-1));
//            yard.trim().substring(0, yard.trim().length() - 1)) * 0.9144f;
        } else {
            return Float.parseFloat(yard);
        }
    }

    /**
     * 根据数量返回float的数量数组
     * @param quantityStr
     * @return 每匹分别多少米
     */
    public static float[] calculateQuantitys(String quantityStr) {
        float[] quantitys = null;
        String[] split = quantityStr.split("\\+");

        quantitys = new float[split.length];

        for (int i = 0; i < quantitys.length; i++) {
//            quantitys[i] = Float.parseFloat(split[i]);
            quantitys[i] = calculateAotoYardToMi(split[i]);
        }


        return quantitys;
    }

    /**
     *根据所需的参数个数,返回.
     * @param argsSize 参数个数
     * @return  假设 argsSize = 3 那么返回 "?, ?, ?"
     */
    public static String calculateArgsSize(int argsSize) {
        StringBuffer s = new StringBuffer();

        for (int i = 0; i < argsSize; i++) {
            if (i == argsSize - 1) {
                s.append("?");
            } else {
                s.append("?, ");
            }
        }

        return s.toString();
    }

    /**
     * 格式化日期
     * @param dateStr
     * @return
     */
    public static Date calculateFormatDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException ex) {
            Logger.getLogger(CalculateUtil.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("日期格式有误.");
            return null;
        }
    }

    /**
     * 根据日期返回默认的日期格式
     * @param date
     * @return yyyy-MM-dd
     */
    public static String calculateFormatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 根据指定的 日期格式 返回日期
     * @param date
     * @param partten 格式
     * @return 日期String
     */
    public static String calculateFormatDate(Date date, String partten) {
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        return sdf.format(date);
    }

    /**
     * 返回处理前的数量
     * 处理后:50+12+81+51 = 610   处理前:50+12+81+51
     * @param quantity
     * @return 类似50+12+81+51
     */
    public static String calculateOriginQuantity(String quantity) {
        int index = quantity.indexOf("=");
        return quantity.substring(0, index - 1);
    }

    public static void main(String[] args) {
        String quantitys = "100+100+100+100+50 = 450$44";
        String sss = "abc";
        System.out.println(sss.substring(0, 1));

        String[] ss = quantitys.split("\\$");

        System.out.println(calculateOriginQuantity(quantitys));

        System.out.println(calculateQuantity(calculateOriginQuantity(quantitys)));



//        float[] fs = calculateQuantitys(quantitys);
//        for(float f:fs) {
//            System.out.println(f);
//        }

//        String str = "10,15,10";

//        System.out.println(calculateQuantity(str));

//        System.out.println(calculateArgsSize(5));
//        String sql = "INSTER INTO " + "test" + " values " + "(" + CalculateUtil.calculateArgsSize(5) + ")";
//        System.out.println(sql);

    }
}
